package com.juzipi.demo.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 自定义realm实现
 * 将认证/授权的数据转为数据库的实现
 */

public class CustomerRealm extends AuthorizingRealm {



    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {



        return null;
    }



    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //在token中获取用户名
        String principal = (String)authenticationToken.getPrincipal();
        System.out.println("身份信息"+principal);
        //根据身份信息使用mybatis查询数据库
        if ("guest".equals(principal)){
            //参数一：返回数据库中正确的用户名
            //参数二：返回数据库中的正确密码
            //参数三：提供当前realm的名字 用的父类里的getName()
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal,"guest",getName());

            return simpleAuthenticationInfo;
        }

        return null;
    }
}
